
/**   
* @Title: SecureCenterInterceptor.java 
* @Package cn.net.vote.data.interceptor
* @Description: TODO(用一句话描述该文件做什么) 
* @author Tianxy
* @date 2017年8月25日 下午3:51:15 
* @version V1.0   
*/ 

package cn.net.vote.data.interceptor;

import java.util.HashMap;
import java.util.Map;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.impl.TextCodec;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Base64Utils;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.net.vote.data.filter.ParameterRequestWrapper;
import cn.net.vote.util.ConfigUtil;


/** 
 * @author Tianxy
 */
public class SecureCenterInterceptor  extends HandlerInterceptorAdapter{
	protected Logger logger =  LoggerFactory.getLogger(this.getClass());
	
	@Override
	    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
	        String authorization = request.getHeader("Authorization");
	        logger.debug("The authorization is: {}",authorization);
	        if("true".equals(ConfigUtil.getString("custom.secureCenter.isValidationToken"))){
	        	if(authorization==null&&"".equals(authorization)){
	        		//throw new WebApplicationException(HttpServletResponse.SC_UNAUTHORIZED);//抛出未认证的错误
	        	}else{
	        		//Jws<Claims> claims = Jwts.parser().isSigned(authorization);
	        		//如果符合签发格式
	        		if(Jwts.parser().isSigned(authorization)){
	        			String PayloadString = authorization.split("\\.")[1];
	        			String jsonStr  =new String( Base64Utils.decodeFromString(PayloadString)) ;
	        			JSONObject  obj = JSON.parseObject(jsonStr);
	        			//logger.info("groupcode:"+obj.getString("groupcode")+"   utype:"+obj.getString("utype"));
	        		}
		        	
	        	}
	        
	        }
	        return super.preHandle(request, response, handler);
	    }
	 public static void main(String[] args) {
		String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI0MmY1ZDlkZC1voteDIwLTRmZmMtOWMzNi0zMmI3YWUxMjdkMTgiLCJpYXQiOjE1MDM2NTM0ODIsInN1YiI6IjE4NiIsImlzcyI6IlNlY3VyZSBDZW50ZXIiLCvotecm91cGNvZGUiOiIyMzAwMDAwMDAwMDAiLCJ1dHlwZSI6IjEiLCJleHAiOjE1MDM2NTcwODJ9.15i3TH4-EVJMuaUoohSHmQQopug_naFnCJlL55AMP7M";
		String tokens[] = token.split("\\.");
		String secToken ="eyJqdGkiOiIzYzdhN2UzNy0yMWUyLTRhMTYtOTUwNS0zMjVlMjI0NGMxZjAiLCJpYXQiOjE1MDM4ODc5MDcsInN1YiI6IjEiLCJpc3MiOiJTZWN1cml0eSBDZW50ZXIiLCvotecm91cGNvZGUiOiIyMzAwMDAwMDAwMDAiLCJ1dHlwZSI6IjEifQ";
		//eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI0MmY1ZDlkZC1voteDIwLTRmZmMtOWMzNi0zMmI3YWUxMjdkMTgiLCJpYXQiOjE1MDM2NTM0ODIsInN1YiI6IjE4NiIsImlzcyI6IlNlY3VyZSBDZW50ZXIiLCvotecm91cGNvZGUiOiIyMzAwMDAwMDAwMDAiLCJ1dHlwZSI6IjEiLCJleHAiOjE1MDM2NTcwODJ9.15i3TH4-EVJMuaUoohSHmQQopug_naFnCJlL55AMP7M
		// Jwts.parser().parse(token).getBody();
		// BASE64Decoder decoder = new BASE64Decoder();
		// Claims claims  = Jwts.parser().parseClaimsJws(token);
		//System.out.println(Jwts.parser().parseClaimsJws(token));
		System.out.println( TextCodec.BASE64.decodeToString(secToken) );
	 }
}
